//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    flagShow:false,    //统计表是否显示标志
    title: '六点骰',
    buttonContent:'投掷',
    pointText: '次 数 : ',
    minutes: '0' + 0,  //右上角计时器分
    seconds: '0' + 0,  //右上角计时器秒
    intervalNum: 0,    //计时器清空数字
    pointCount: 0,     //最下面次数
    buttonShow:true,
    timer: null,       //骰子点数定时器返回数字
    pointNum: 0,
    finalNum: 0,
    resetNo: 0,
    times: 0,
    buttonActive:'../../images/btn1.png',
    buttonNonActive: '../../images/btn0.png',
    arr:[
      '../../images/01.png',
      '../../images/02.png',
      '../../images/03.png',
      '../../images/04.png',
      '../../images/05.png',
      '../../images/06.png'
    ],
    statisTable:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      pointImg:this.data.arr[Math.floor(Math.random() * 6 )]
    })
  },

  btnClick: function(event) {
    // 点击投掷按钮，震动
    wx.vibrateLong({
      success: res => {
      },
      fail: err => {
      }
    })
    let that = this;

    // 计数器
    let pointCount = that.data.pointCount;
    pointCount++;
    that.setData({
      times: 0,
      pointCount: pointCount
    })

    // 投掷点数计算
    that.data.timer = setInterval(function(){
      let num = Math.floor(Math.random() * 6);
      let finalNum = num + 1;
      let times = that.data.times;
      times++;
      that.setData({
        pointImg: that.data.arr[num],
        finalNum: finalNum,
        times:times,    
        buttonShow:false  //投掷按钮变成灰色
      });

      if (that.data.times >= 15) {
        clearInterval(that.data.timer);
        that.setData({
          pointImg: that.data.arr[num],
          pointNum: that.data.finalNum,
          buttonShow: true  //投掷按钮变成绿色
        }) 
      }
    },100);

    // 计时器调用
    that.timeMeter();

  },

  // 计时器
  timeMeter: function() {
    const that = this;
    let intervalNum = this.data.intervalNum;
    if (intervalNum == 0) {
      intervalNum = setInterval(function () {
        that.calcTime();
      }, 1000);
      that.setData({
        intervalNum: intervalNum
      })      
    }
  },

  //时间计算
  calcTime: function() {
    const that = this;
    let seconds = this.data.seconds;
    let minutes = this.data.minutes;    
    seconds++;
    // 如果秒大于0，向分进1位
    if (seconds >= 60) {
      seconds = 0;
      minutes++;
      // 如果分大于60，分置为0
      if (minutes >= 60) {
        minutes = 0;
      }
      // 如果分小于10，10位补零
      if (minutes < 10) {
        that.setData({
          minutes: '0' + minutes
        })
      } else {
        that.setData({
          minutes: minutes
        })
      }
    }
    // 如果秒小于10，10位补零
    if (seconds < 10) {
      that.setData({
        seconds: '0' + seconds
      })
    } else {
      that.setData({
        seconds: seconds
      })
    }
  },

  // 点击计时器
  timeMeterClick: function() {
    const that = this
    that.setData({
      flagShow: true
    })
  },

  // 点击计数器
  pointClick: function() {
    const that = this
    that.setData({
      flagShow: true
    })
  },

  // 关闭统计表
  statisClose: function() {
    const that = this
    that.setData({
      flagShow: false
    })
  },

  // 点击清空按钮
  clearClick: function() {
    const that = this;
    wx.showModal({
      title: '',
      content: '是否进行清空操作',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let statisTable = that.data.statisTable;
          statisTable.length = 0; //清空统计表的内容（通过设置统计表的长度）
          that.setData({
            resetNo: 0,  //清空以后，序号从0开始计数
            statisTable: statisTable
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  // 点击重置按钮
  resetClick: function() {
    const that = this;   
    wx.showModal({
      title: '',
      content: '是否进行重置操作',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let resetNo = that.data.resetNo;
          resetNo++;
          that.setData({
            resetNo: resetNo
          })
          let statisTable = that.data.statisTable;
          if (statisTable.length >= 99) {
            wx.showToast({
              title: '超出统计限制，请清空统计表',
            })
          } else {
            let obj = {
              tableNo: resetNo,
              time: that.data.minutes + ":" + that.data.seconds,
              count: that.data.pointCount
            }
            statisTable.push(obj);
            that.setData({
              statisTable: statisTable
            });
            // 计时器清零
            let intervalNum = that.data.intervalNum;
            clearInterval(intervalNum);
            that.setData({
              minutes: '00',
              seconds: '00',
              intervalNum: 0,
              pointCount: 0
            });
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
          return
        }
      }
    })
  }
})